﻿using System;
using System.Linq;
using System.Text;
using Biz.Buffer;
using Biz.EF.Platform.Kq;

using Hjt212Parser;
using log4net;

namespace Biz.Process
{
    public class ProcessorMonthPlan
    {
        private ILog log = LogManager.GetLogger(typeof (ProcessorMonthPlan));
        public void GenMonthPlan(object state)
        {
            var sb = new StringBuilder();
            while (true)
            {
                var now = DateTime.Now;
                if (now.Day == 1)
                {
                    try
                    {
                        var daysOfMonth = (DateTimeHelper.NextMonth(now) - DateTimeHelper.MonthBegin(now)).Days;
                        var daysOfQuarter = (DateTimeHelper.NextQuarter(now) - DateTimeHelper.QuarterBegin(now)).Days;
                        var daysOfYear = (DateTimeHelper.NextYear(now) - DateTimeHelper.YearBegin(now)).Days;
                        //生成月计划指令下发
                        //using (var gCtx = new GatewayMonitorContext())
                        using (var pCtx = new PlatformYgContext())
                        {
                            var ports = CommonInformation.GetAllLetports();
                            foreach (var port in ports)
                            {
                                var licence =
                                    CommonInformation
                                        .GetAllLicencess()
                                        .Where(t => t.State_Flag.Equals("有效"))
                                        .OrderByDescending(t => t.EffectDate)
                                        .FirstOrDefault(t => t.Pollution_Source_ID == port.Pollution_Source_Id);
                                sb.Length = 0;
                                if (licence?.Waster_Day_Let_Measure != null)
                                {
                                    sb.Append("B01-MonAll=" + (licence.Waster_Day_Let_Measure * daysOfMonth).Value.ToString("F0"));
                                    sb.Append(",B01-QutAll=" + (licence.Waster_Day_Let_Measure * daysOfQuarter).Value.ToString("F0"));
                                    sb.Append(",B01-YeaAll=" + (licence.Waster_Day_Let_Measure * daysOfYear).Value.ToString("F0"));
                                    Envi_Transmittal_Plan plan = new Envi_Transmittal_Plan
                                    {
                                        QN = DateTime.Now.ToString("yyyyMMddHHmmssfff"),
                                        ST = "32",
                                        CN = "3093",
                                        PW = "123456",
                                        MN = port.Data_Collection_Instrument_SN,
                                        Flag = "1",
                                        PNO = "",
                                        PNUM = "",
                                        Type_ID = "1",
                                        Flag_process = "0",
                                        Reason = null,
                                        StartTime = DateTimeHelper.MonthBegin(now).ToString("yyyyMMddHHmmsss"),
                                        EndTime = DateTimeHelper.NextMonth(now).ToString("yyyyMMddHHmmsss"),
                                        ExecCount = 0,
                                        ExecSuccess = false,
                                        CP = sb.ToString()
                                    };
                                    pCtx.Envi_Transmittal_Plan.Add(plan);
                                    pCtx.SaveChanges();
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error( ex);
                    }
                }
                var span = (DateTime.Now - now).TotalSeconds;
                var milliSeconds = 24 * 60 * 60 * 1000 - (int)span;
                if (milliSeconds < 0)
                {
                    milliSeconds = 10;
                }
                ThreadUtil.Sleep(milliSeconds);
            }
            // ReSharper disable once FunctionNeverReturns
        }
    }
}
